iT邦幫忙

2023 iThome 鐵人賽

DAY 26
0
自我挑戰組

馬克的 Kali Linux 與資安學習小筆記系列 第 26

30-26 Docker - Security Best practices

  • 分享至 

  • xImage
  •  

https://docs.docker.com/develop/security-best-practices/

Choose the right base image

在 docker hub 中有大約 8.3 million 的 repository,那要如何選擇呢 ? 文章有幾個建議 :

  • 選擇 docker official Images。

  • 選擇被驗證的 publisher。
    https://ithelp.ithome.com.tw/upload/images/20231011/20089358MFZm1bKMf0.png

  • 在你要建立自已服務的 image 時,選擇較小的基礎 image,它可以快速下截、縮小 image 大小,並且因為小,所以依賴引用的漏洞數量也小,所以有助於安全。例如 alpine 系列。

Use multi-stage builds

multi-stage builds 是 docker 一個特別,它允許你在一個 dockerfile 中定義多個建構階段,每個階段可以用來執行特定任務,並且最終僅保留你需要的檔案,從而減少 image 的大小。

這也符合上面的建議,以最小的 image 為基礎,就能減少越多資安漏洞。

以下為一個範例 :

# 開發階段
FROM node:alpine as development

# 將應用程式程式碼複製到映像中
COPY . /app

# 在開發階段可以包含額外的工具和依賴
RUN npm install
RUN npm run build

# 生產階段
FROM node:alpine as production

# 從開發階段複製構建好的應用程式
COPY --from=development /app/dist /app

# 生產階段不需要開發工具和額外的依賴
RUN npm install --only=production

# 定義應用程式啟動命令
CMD ["npm", "start"]

Rebuild images

通常我們在執行 docker build 時,都會有 cache,然後這裡的建議就是,『 定期的 rebuild image 』,也就是執行以下如下指令 :

$ docker build --no-cache -t myImage:myTag myPath/

Check your image for vulnerabilities

就是檢查你的 image 與你用的 base image 的弱點,docker 有提供以下的工具 :


上一篇
30-25 Google GKE Security ( 3 ) - Network Security + workloads Security
下一篇
30-27 OWASP Docker Security ( 1 )
系列文
馬克的 Kali Linux 與資安學習小筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言